package com.qzing.apijson.dcp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qzing.apijson.dcp.edu.dto.ApiInfo;
import com.qzing.apijson.dcp.edu.dto.StudentDTO;
import com.qzing.apijson.dcp.edu.model.Student;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName JSONConverter
 * @Description TODO
 * @Author liucs
 * @Date 2021-03-29
 * @Version V1.0
 */
public class JSONConverter {


    /**
     * 将
     * @param studentDTO
     * @return
     */
    public static String convertStudent(StudentDTO studentDTO){
        ApiInfo apiInfo=new ApiInfo();
//        StudentDTO inParam=new StudentDTO();

        apiInfo.setInParam(studentDTO);
//        StudentDTO outParam=new StudentDTO();

        Map<String,String> outField=new HashMap<>(16);
        outField.put("id","name");
        apiInfo.setOutField(outField);

        String jsonStr=apijson.JSONObject.toJSONString(apiInfo);
        System.out.println("parse json str:"+jsonStr);
        return jsonStr;
    }

    public static void main(String[] args) {
//        doParam();
      /*  StudentDTO studentDTO=new StudentDTO();
        studentDTO.setSex(1);
        String json = convertStudent(studentDTO);
        System.out.println("convert result:"+json);*/
        parseReqParam();
    }
    static String reqJson="{\n" +
            "  \"topic\": \"edu\",\n" +
            "  \"action\": \"GET\",\n" +
            "  \"version\": \"1.0\",\n" +
            "  \"catalog\": \"student\",\n" +
            "  \"ip\": \"192.168.1.128\",\n" +
            "  \"port\": 8080,\n" +
            "  \"context\": \"/edu\",\n" +
            "  \"describe\": \"学生数据信息查询服务API\",\n" +
            "  \"inParam\": {\"classId\":101},\n" +
            "  \"outField\": {\n" +
            "    \"sorted\": 1,\n" +
            "    \"column\": \"id,name,sex,age,grade,classId,address\"\n" +
            "  }\n" +
            "}";

    /**
     * 解析请求参数
     * @return
     */
    private static void parseReqParam(){
        JSONObject jsonObject=  JSON.parseObject(reqJson);

       String topic= jsonObject.getString("topic");
       Object inParam= jsonObject.get("inParam");
       Object outField = jsonObject.get("outField");

        System.out.println("get api-topic:"+topic);
        System.out.println("get api-inParam:"+inParam);
        System.out.println("get api-outField:"+outField);

    }
    private static void doParam() {
        Map<String,Object> reqMap=new HashMap<>();
//        reqMap.put("Student","");
//        reqMap.put("classId","101");
        Student student=new Student();
        student.setClassId(102);
        student.setName("张三");
        reqMap.put("Student",student);
        String str = JSONObject.toJSONString(reqMap);
        System.out.println(str);

        Map<String,Object> reqMap2=new HashMap<>();
        reqMap2.put("[]",reqMap);
        String reqMap2Str = JSONObject.toJSONString(reqMap2);
        System.out.println(reqMap2Str);
    }
}
